#include "MCParallelConstraintVisual.h"
#include <MCParallelConstraint.h>
#include <MCSettings.h>

MCParallelConstraintVisual::MCParallelConstraintVisual(ICanvas *presenter, MCParallelConstraint *constraint) :
    MCConstraintVisual(presenter, constraint),
    _constraint(constraint),
    _segv1(constraint->_segment1),
    _segv2(constraint->_segment2)
{
}

void MCParallelConstraintVisual::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    setPainter(painter);

    double size = MCSettings::LabelSize;

    drawLabel(transformedPoint1(), size, size, "resources/parallel.png");
    drawLabel(transformedPoint2(), size, size, "resources/parallel.png");
}

QPainterPath MCParallelConstraintVisual::shape() const
{
    QPainterPath res;
    res.connectPath(labelBoundings(transformedPoint1()));
    res.connectPath(labelBoundings(transformedPoint2()));

    return res;
}

void MCParallelConstraintVisual::setHighlight(ItemHighlight highlight)
{
    MCGraphicsItem::setHighlight(highlight);

    if (highlight == ItemHighlight::Hovered)
    {
        _segv1->hoverEnter();
        _segv2->hoverEnter();
    }
}

MCPoint2D MCParallelConstraintVisual::transformedPoint1() const
{
    MCPoint2D midPoint = _segv1->lineSegment().midPoint();
    return presenter->screenToWorld(midPoint + labelOffset);
}

MCPoint2D MCParallelConstraintVisual::transformedPoint2() const
{
    MCPoint2D midPoint = _segv2->lineSegment().midPoint();
    return presenter->screenToWorld(midPoint + labelOffset);
}
